//
//  FirstViewController.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/24.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXTotalOrderController.h"
#import "GXNotOrderViewCell.h"
#import "GXNotArriveViewCell.h"
#import "GXCancelViewCell.h"
#import "OrderAndUploadPhotoMananger.h"
#import "GXCommonOrderDetailController.h"
@interface GXTotalOrderController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * totalTableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign) int addStartNumber;
@end
static NSString *const notOrderViewCell=@"GXNotOrderViewCell";
static NSString *const notArriveViewCell=@"GXNotArriveViewCell";
static NSString *const cancelViewCell=@"GXCancelViewCell";
@implementation GXTotalOrderController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatAllView];
}
- (void)creatAllView
{
    [self.view addSubview:self.totalTableView];
    self.totalTableView.mj_header =[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.totalTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.totalTableView.mj_header beginRefreshing];
}
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark - loadData
- (void)refreshData
{
    _addStartNumber = 0;
    [self requestDataSourceWithStartIndex:_addStartNumber];
}
- (void)loadMoreData
{
    [self requestDataSourceWithStartIndex:++_addStartNumber];
}
- (void)requestDataSourceWithStartIndex:(int)startIndex
{
    if (startIndex == 0) {
        [self.dataArray removeAllObjects];
    }
    NSString *pageStr = [NSString stringWithFormat:@"%d",startIndex];
    [OrderAndUploadPhotoMananger selectShopOrderWithStoreID:StoreId state:@" " page:pageStr success:^(id responseObject) {
        if (responseObject) {
            [self.dataArray addObjectsFromArray:responseObject];
            [self.totalTableView reloadData];
        }
        [self.totalTableView.mj_header endRefreshing];
        [self.totalTableView.mj_footer endRefreshing];
    } fail:^(id responseObject) {
        [self.totalTableView.mj_header endRefreshing];
        [self.totalTableView.mj_footer endRefreshing];
    }];
}
#pragma mark -- 创建uitableview
- (UITableView *)totalTableView
{
    if (_totalTableView == nil) {
        _totalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,kUIScreenWidth,kUIScreenHeight-Height_TabBar-Height_StatusBar-43) style:UITableViewStyleGrouped];
        _totalTableView.delegate=self;
        _totalTableView.dataSource=self;
        _totalTableView.sectionFooterHeight = 0.01f;
        _totalTableView.sectionHeaderHeight= 15;
        _totalTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _totalTableView.showsHorizontalScrollIndicator=NO;
        _totalTableView.showsVerticalScrollIndicator=NO;
        _totalTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _totalTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_totalTableView registerNib:[UINib nibWithNibName:@"GXNotOrderViewCell" bundle:nil] forCellReuseIdentifier:notOrderViewCell];
        [_totalTableView registerNib:[UINib nibWithNibName:@"GXNotArriveViewCell" bundle:nil] forCellReuseIdentifier:notArriveViewCell];
        [_totalTableView registerNib:[UINib nibWithNibName:@"GXCancelViewCell" bundle:nil] forCellReuseIdentifier:cancelViewCell];
    }
    return _totalTableView;
}
#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderMessageModel * model = self.dataArray[indexPath.section];
    if ([model.state isEqualToString:@"0"]) {
        GXNotOrderViewCell * notAcceptCell = [tableView dequeueReusableCellWithIdentifier:notOrderViewCell];
        notAcceptCell.model = model;
        return notAcceptCell;
    }
    if ([model.state isEqualToString:@"1"]) {
        GXNotArriveViewCell* acceptCell = [tableView dequeueReusableCellWithIdentifier:notArriveViewCell];
        acceptCell.model = model;
        return acceptCell;
    }
    if ([model.state isEqualToString:@"2"]||[model.state isEqualToString:@"3"]) {
        GXCancelViewCell * cancelCell = [tableView dequeueReusableCellWithIdentifier:cancelViewCell];
        cancelCell.model = model;
        return cancelCell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 235;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GXCommonOrderDetailController * detailVC = [[GXCommonOrderDetailController alloc]init];
    OrderMessageModel *model = self.dataArray[indexPath.section];
    if (model) {
        detailVC.onumber = [NSString stringWithFormat:@"%@",model.oNumber];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
@end
